<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <p class="my-p"></p>
    <script>
        // 1.创建Promise对象
        const p = new Promise((resolve, reject) => {
            // 2.执行XHR代码
            const xhr = new XMLHttpRequest()
            xhr.open('GET', 'http://hmajax.itheima.net/api/province')
            xhr.addEventListener('loadend', () => {
                console.log(xhr);
                console.log(xhr.response);
                // 判断响应状态码
                if (xhr.status >= 200 && xhr.status < 300) {
                    resolve(JSON.parse(xhr.response))
                } else {
                    reject(new Error(xhr.response))
                }
            })
            xhr.send()
        })

        // 3.关联成功或者失败函数
        p.then(result => {
            console.log(result)
            document.querySelector('.my-p').innerHTML = result.list.join('<br>')
        }).catch(error => {
            console.dir(error)
            // 显示返回的错误提示信息
            document.querySelector('.my-p').innerHTML = error.message
        })

    </script>

</body>

</html>